package com.icss.train_sys_back.mapper.system_manage;

import com.icss.train_sys_back.entity.home.OrderEntity;
import com.icss.train_sys_back.entity.home.OrderPassengerDTO;
import com.icss.train_sys_back.entity.home.PassengerEntity;
import com.icss.train_sys_back.entity.home.PassengerUserEntity;
import com.icss.train_sys_back.entity.system_manage.UserEntity;
import org.apache.ibatis.annotations.*;

import java.time.LocalDate;
import java.util.List;

/**
 * @Author 小泽
 * @createTime 2025/9/16 14:19
 */
@Mapper
public interface UserMapper {

    //用户登录
    @Select("select * from t_user where username = #{username} ")
    UserEntity login(UserEntity user);

    //后台管理员条件获取所有用户信息
    @Select("<script>" +
            "select * from t_user u left join t_role r on u.role_id=r.role_id " +
            "<where>" +
            "<if test='username!=null and username!=\"\"'>" +
            " and username like CONCAT('%',#{username},'%')</if>" +
            "<if test='name!=null and name!=\"\"'>" +
            "and name like CONCAT('%',#{name},'%')</if>" +
            "<if test='roleId!=null and roleId!=\"\"'>and u.role_id=#{roleId}</if>" +
            "<if test='status!=null and status!=\"\"'>and u.status=#{status}</if>" +
            "</where >" +
            "</script>")
    @Results(
            @Result(property = "role.roleName", column = "role_name")
    )
    List<UserEntity> getAllUser(String username, String name, Integer roleId, Integer status);

    //添加用户
    @Insert("insert into t_user values(null,#{username},#{password},#{name},#{sex},#{birthday},#{phone},#{email},#{status},#{roleId},now(),now())")
    boolean addUser(UserEntity user);

    //修改用户
    @Update("update t_user set username=#{username},password=#{password},name=#{name},sex=#{sex},birthday=#{birthday},phone=#{phone},email=#{email},status=#{status},role_id=#{roleId},update_time=now() where user_id=#{userId}")
    boolean updateUser(UserEntity user);

    //根据id删除某位用户
    @Delete("delete from t_user where user_id=#{userId}")
    boolean deleteUser(Integer userId);

    //根据用户id修改状态
    @Update("update t_user set status=#{status} where user_id=#{userId}")
    boolean changeStatus(Integer userId, Integer status);

    //检查用户名是否存在
    @Select("select * from t_user where username = #{username}")
    UserEntity checkUserName(String username);

    //检查手机号是否存在
    @Select("select * from t_user where phone = #{phone}")
    UserEntity checkPhone(String phone);

    //检查邮箱号是否存在
    @Select("select * from t_user where email = #{email}")
    UserEntity checkEmail(String email);

    //用户注册
    @Insert("insert into t_user values (null,#{username},#{password},#{name},#{sex},#{birthday},#{phone},#{email},null,null,null,now(),now())")
    @Options(useGeneratedKeys = true, keyProperty = "userId")
    Integer register(UserEntity user);

    //更新人脸注册状态
    @Update("update t_user set face_register = 1 where user_id = #{userId}")
    void updateFaceRegisterStatus(Integer userId);

    //根据用户Id查询乘车人列表
    @Select("select * from t_passenger where user_id = #{userId}")
    List<PassengerEntity> getPassengerListByUserId(Integer userId);

    //检查用户是否登录
    @Select("select * from t_user where user_id=#{userId}")
    UserEntity checkLogin(Integer userId);

    //根据用户Id查询用户信息
    @Select("select * from t_user where user_id = #{userId}")
    UserEntity getUserInfoByUserId(Integer userId);

    //更新用户部分信息
    @Update("update t_user set email = #{email},phone = #{phone},username = #{username}  where user_id = #{userId}")
    boolean updateUserEmailAndPhoneAndUserNameByUserId(UserEntity user);

    //添加乘车人
    @Insert("insert into t_passenger values (null,#{userId},#{passengerName},#{idCard},#{phone},#{passengerType},#{status},now(),null)")
    @Options(useGeneratedKeys = true, keyProperty = "passengerId")
    Integer addPassenger(PassengerEntity passenger);

    //更新乘车人
    @Update("update t_passenger set user_id = #{userId},passenger_name = #{passengerName} ,id_card = #{idCard}," +
            "phone = #{phone},passenger_type = #{passengerType},update_time = now() where passenger_id = #{passengerId}")
    Integer updatePassenger(PassengerEntity passenger);

    //删除乘车人
    @Delete("delete from t_passenger where passenger_id = #{passengerId}")
    Integer deletePassenger(Integer passengerId);

    //根据手机号查询用户
    @Select("select * from t_user where phone = #{phone}")
    UserEntity findUser(String phone);

    //向乘车人用户表中添加乘车人和对应用户的id
    @Insert("insert into t_passenger_user values (null,#{passengerId},#{userId})")
    Integer addPassengerUser(Integer passengerId, Integer userId);

    //删除乘车人用户表中添加乘车人和对应用户的id
    @Delete("delete from t_passenger_user where t_passenger_id=#{passengerId}")
    Integer deletePassengerUser(Integer passengerId);

    //删除order_passenger信息
    @Delete("delete from t_order_passenger op where passenger_id = #{passengerId} and order_id = #{orderId}")
    int deleteRecordByUserId(Integer passengerId, String orderId);

    //根据手机号查询用户信息
    @Select("select * from t_user where phone = #{phone}")
    UserEntity getUserInfoByPhone(String phone);

    //根据userId  查对应的自己的passenger身份
    @Select("select * from t_passenger p left join t_passenger_user pu  on p.passenger_id = pu.t_passenger_id where pu.t_user_id = #{userId}")
    PassengerEntity findPassengerInfoByUserId(Integer userId);

    //人脸信息更改时验证密码
    @Select("select * from t_user where user_id = #{userId} and password = #{password}")
    UserEntity verifyPwd(Integer userId, String password);


    List<OrderEntity> getHistoryOrderList(Integer userId, LocalDate startTime, LocalDate endTime, String orderId);


}
